#!/usr/bin/env python
#coding:utf-8
#copy from:http://www.keakon.net/2009/07/02/%E7%94%A8PyQt4%E6%89%93%E9%80%A0%E4%B8%80%E4%B8%AA%E7%AE%80%E6%98%93%E6%B5%8F%E8%A7%88%E5%99%A8

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class MyBrowser(QWidget):

    def __init__(self, parent = None):
        super(MyBrowser, self).__init__(parent)
        self.createLayout()
        self.createConnection()
        self.webView.load(QUrl('http://www.google.com'))

    def search(self):
        address = str(self.addressBar.text())
        if address:
            if address.find('://') == -1:
                address = 'http://' + address
            url = QUrl(address)
            self.webView.load(url)

    def createLayout(self):
        self.setWindowTitle("keakon's browser")

        self.addressBar = QLineEdit()
        self.goButton = QPushButton("&GO")
        bl = QHBoxLayout()
        bl.addWidget(self.addressBar)
        bl.addWidget(self.goButton)

        self.webView = QWebView()

        layout = QVBoxLayout()
        layout.addLayout(bl)
        layout.addWidget(self.webView)

        self.setLayout(layout)

    def createConnection(self):
        self.connect(self.addressBar, SIGNAL('returnPressed()'), self.search)
        self.connect(self.addressBar, SIGNAL('returnPressed()'), self.addressBar, SLOT('selectAll()'))
        self.connect(self.goButton, SIGNAL('clicked()'), self.search)
        self.connect(self.goButton, SIGNAL('clicked()'), self.addressBar, SLOT('selectAll()'))


app = QApplication(sys.argv)

browser = MyBrowser()
browser.show()

sys.exit(app.exec_())